package co.yixiang.hw.rest;

import cn.hutool.http.HttpUtil;
import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.hw.utils.WXCore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "微信api相关接口")
@RestController
@RequestMapping("/api/wx")
public class WxApiController {

    private final static String appid = "wx44031547d2d62ced";
    private final static String appSecret = "ae02af89067c92af227c347150aadf94";
    /**
     * 获取url请求前缀
     * @explain http://localhost:8080/test
     * @param request request对象
     * @return
     */
    protected static String getRequestPrefix(HttpServletRequest request) {
        // 网络协议
        String networkProtocol = request.getScheme();
        // 网络ip
        String ip = request.getServerName();
        // 端口号
        int port = request.getServerPort();
        // 项目发布名称
        String webApp = request.getContextPath();
        String urlPrefix = networkProtocol + "://" + ip + ":" + port + webApp;
        return urlPrefix;
    }

    @PostMapping("decrypt")
    @ApiOperation(value = "获取decrypt")
    @AnonymousAccess
    public ResponseEntity<Object> decrypt(
            @ApiParam(
                    name = "params",
                    value = "{\n" +
                            "  \"appId\": \"1\",\n" +
                            "  \"encryptedData\": \"xxx\",\n" +
                            "  \"sessionKey\": \"aes\",\n" +
                            "  \"iv\": \"a\"\n" +
                            "}",
                    required = true)
            @RequestBody JSONObject params) {
        String code = params.getString("code");
        String sessionKey = getSessionKey(code);
        String result = WXCore.decrypt(appid, params.getString("encrypdata"), sessionKey, params.getString("ivdata"));
        log.error("微信接口decrypt 参数：" + JSON.toJSONString(params)+"\n结果："+result);
        return new ResponseEntity<>(JSON.parse(result),HttpStatus.OK);
    }

    @PostMapping("jscode2session")
    @ResponseBody
    @ApiOperation(value = "获取jscode2session")
    @AnonymousAccess
    public ResponseEntity<Object> getSessionKey(@RequestBody Map params){
//        params.put("appid","wx04eefcc476dc47e0");//和院好物
//        params.put("secret","f5b08532e8c794438e7c9bd89cf95aad"); //和院好物
        params.put("appid","wx44031547d2d62ced");  //抱果下山
        params.put("secret","ae02af89067c92af227c347150aadf94"); //抱果下山
        String s = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", params);
        log.error("微信接口jscode2session 参数：" + JSON.toJSONString(params)+"\n结果："+s);
        return new ResponseEntity<>(JSON.parse(s),HttpStatus.OK);
    }

    private String getSessionKey(String code){
        JSONObject params = new JSONObject();
        params.put("js_code",code);
        params.put("appid",appid);  //抱果下山
        params.put("secret",appSecret); //抱果下山
        String s = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", params);
        JSONObject result = JSONObject.parseObject(s);
        log.error("微信接口jscode2session 参数：" + JSON.toJSONString(params)+"\n结果："+s);
        return result.getString("session_key");
    }
}
